<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Facades\DB;
use Validator;

class CheckAppId
{
    /** 验证公众号是否存在、启用
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        $validate = Validator::make($request->only('appid'),[
            'appid' => 'required'
        ]);
        if($validate->fails()){
            exit(json_encode(['code'=>400,'message'=>$validate->errors()->first()]));
        }
        $app_info = DB::table('APPS')->where('appid',$request['appid'])->first();

        if(!$app_info){
            exit(json_encode(['code'=>404,'message'=>'公众号不存在']));
        }
        if($app_info->status == 0){
            exit(json_encode(['code'=>404,'message'=>'公众号已被禁用']));
        }
        return $next($request);
    }
}
